function unmark2(elementID){
    document.getElementById(elementID).className='input';
    var boxID = 'err_'+elementID;
    if (document.getElementById(boxID) != null){
	var box = document.getElementById(boxID);
	box.parentNode.removeChild(box);
	document.getElementById(elementID).className='';
    }
}
function mark2(elementID, HTMLtex){
    var target = document.getElementById(elementID);
    var boxID = 'err_'+elementID;
    if (document.getElementById(boxID) != null) unmark2(elementID);
    target.className='text_error';
//    target.insertAdjacentHTML('afterEnd', '<span id='+boxID+'>'+HTMLtex+ '</span>');
}
function operatedInput(inputID,mask){
    var input = document.getElementById(inputID);
    input.onkeypress = check;
    function check(e){
	var evt = (e) ? e : window.event;
	var code = (document.all) ? evt.keyCode:evt.charCode;
	if ((code == 0) || (code == 8)){return true;}

	var bool = false;
	function doRule(Rnamber){
	    var rule = mask.charAt(Rnamber);
	    switch (rule){
		case '_':
		    return true;
		    break;
		case '`':
		    if ((code < 48) || (code > 57)){
			mark2(inputID,'Только цифры');
//			alert('Только цифры');
			bool = true;
			return false;
		    }
		    else{
			unmark2(inputID);
			bool = false;
			return true;
		    }
		    break;
		case '~':
		    if ((code < 1040) || (code > 1103)){
			mark2(inputID,'Только русские буквы');
//			alert('Только русские буквы');
			bool = true;
			return false;
		    }
		    else{
			unmark2(inputID);
			bool = false;
			return true;
		    }
		    break;
		case '':
		    return false;
		    break;
		default:
		    input.value += rule;
		    if(input.setSelectionRange) {
			var end = input.value.length;
			input.setSelectionRange(end,end);
		    }
		    doRule(Rnamber+1);
		    if(bool){return false;}
		    else{return true;}
		    break;
	    }
	}
	var namber = input.value.length;
	return doRule(namber);
    }
}
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
    HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
    {
	switch (where){
	case 'beforeBegin':
	    this.parentNode.insertBefore(parsedNode,this)
	    break;
	case 'afterBegin':
	    this.insertBefore(parsedNode,this.firstChild);
	    break;
	case 'beforeEnd':
	    this.appendChild(parsedNode);
	    break;
	case 'afterEnd':
	    if (this.nextSibling)this.parentNode.insertBefore(parsedNode,this.nextSibling);
	    else this.parentNode.appendChild(parsedNode);
	    break;
	}
    }

    HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
    {
	var r = this.ownerDocument.createRange();
	r.setStartBefore(this);
	var parsedHTML = r.createContextualFragment(htmlStr);
	this.insertAdjacentElement(where,parsedHTML)
    }


    HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
    {
	var parsedText = document.createTextNode(txtStr)
	this.insertAdjacentElement(where,parsedText)
    }
}
